package com.bdqn.t350.servlet;

import com.alibaba.fastjson.JSON;
import com.bdqn.t350.entity.Page;
import com.bdqn.t350.entity.Pet;
import com.bdqn.t350.service.PetService;
import com.bdqn.t350.service.PetServiceImp;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

@WebServlet(name = "PetListPageServlet", urlPatterns = "/PetListPageServlet")
public class PetListPageServlet extends HttpServlet {

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        resp.setContentType("text/html;charset=UTF-8");
        String currPageNo = req.getParameter("currPageNo");
        if(currPageNo==null){
            currPageNo= "1";
        }
        PetService petService = new PetServiceImp();
        //获得表中总记录数
        int totalCount = petService.tableCountBiz();
        Page page = new Page();
        //打包快递
        page.setCurrPageNo(Integer.parseInt(currPageNo));
        page.setTotalCount(totalCount);
        //获取槟榔列表
        List<Pet> pageList = petService.getPageListBiz(page);
        page.setPetList(pageList);
        //将打包好的快递给快递员
        req.setAttribute("page",page);
        //寄出去
        //req.getRequestDispatcher("listPage.jsp").forward(req,resp);
        PrintWriter out = resp.getWriter();
        out.print(JSON.toJSONString(page));
        out.flush();
        out.close();
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
    }
}
